﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LogParser;
using LogParser.DTO;
using DataStore;
using AppLogic;


namespace LogProcess
{
    class Program
    {
        public static Dictionary<string, ICommandAction> Actions = new Dictionary<string, ICommandAction>()
            {
                { "AddFile", new AddFileCommand() },
                { "AddSite", new AddSiteCommand() },
                { "SQL", new SQLCommand() }            
            };

        static void Main( string[] args )
        {
            if (args.Length == 0)
            {
                ShowHelp();
                return;
            }

            ICommandAction selectedAction = null;
            foreach (var action in Actions)
                if (string.Equals( args[0], action.Key, StringComparison.OrdinalIgnoreCase ))
                    selectedAction = action.Value;

            if (selectedAction != null)
            {
                var parameters = ProcessParameters( args, selectedAction.Parameters );
                if (parameters != null)
                {
                    string error = selectedAction.Process( new CommandParameters( parameters ) );
                    if (!string.IsNullOrWhiteSpace( error ))
                        Console.WriteLine( "Error: " + error );
                    else
                        Console.WriteLine( "Done" );
                }
            }
            else
            {
                ShowHelp();
            }          
        }

        private static List<CommandParameter> ProcessParameters( string[] args, List<CommandParameter> expect )
        {
            for (int i = 0; i < expect.Count; i++)
            {
                if (i + 1 >= args.Length )
                {
                    Console.WriteLine( "Missing parameter: " + expect[i].Name );
                    return null;
                }

                string error = expect[i].SetValue( args[i + 1] );
                if (!string.IsNullOrWhiteSpace(error))
                {
                    Console.WriteLine( "Bad parameter[" + i + "]: " + expect[i].Name + ". " + error);
                    return null;
                }
            }
            return expect;
        }

        private static void ShowHelp()
        {
            Console.WriteLine( "LogProcess" );
            Console.WriteLine( "" );
            Console.WriteLine( "Commands" );
                        
            foreach (var action in Actions)
            {

                Console.WriteLine( "" );
                Console.WriteLine( action.Key + " -- " + action.Value.Description() );
                if (action.Value.Parameters == null)
                    Console.WriteLine( "No parameters" );
                else
                    foreach (var param in action.Value.Parameters)
                        Console.WriteLine( param.Name + " type: " + param.Type );
            }
        }
    }


}
